﻿using System;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Web.Class;

namespace Web.Pages.YongShui._4.ChaoBiaoKZ
{
    public partial class ZhuanHuanCWXX : BasePage
    {
        #region 属性
        public DataTable MyDataTable
        {
            get { return ViewState["MyDataTable"] == null ? null : ViewState["MyDataTable"] as DataTable; }
            set { ViewState["MyDataTable"] = value; }
        }
        #endregion

        #region 变量
        
        
        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _spHelper = new SpHelper();

        YS_CB_HUIZONGCWBLL hzxxbll = new YS_CB_HUIZONGCWBLL();
        /// <summary>
        /// 水表信息对象
        /// </summary>
        YS_CB_SHUIBIAOXX _YS_CB_SHUIBIAOXX = new YS_CB_SHUIBIAOXX();

        //用户信息BLL
        YS_YG_YONGHUXXBLL _YS_YG_YONGHUXXBLL = new YS_YG_YONGHUXXBLL();

        /// <summary>
        /// 用户水表关系BLL
        /// </summary>
        YS_CB_YONGHUSBGXBLL _YS_CB_YONGHUSBGXBLL = new YS_CB_YONGHUSBGXBLL();

        /// <summary>
        /// 水表信息BLL
        /// </summary>
        YS_CB_SHUIBIAOXXBLL _YS_CB_SHUIBIAOXXBLL = new YS_CB_SHUIBIAOXXBLL();

        /// <summary>
        /// 日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件
        
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindgvCuoWuWJM();
            }
            gvCuoWu.DataSource = MyDataTable;
            gvCuoWu.DataBind();
        }
        /// <summary>
        /// 导出excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            string WJM = gvCuoWuWJM.Value == null ? "-1" : gvCuoWuWJM.Value.ToString();
            DataTable YS_CB_ZHUANHUANCW = _spHelper.P_YS_CB_ZHUANHUANCWCX("", WJM).Tables[0];
            if (YS_CB_ZHUANHUANCW != null && YS_CB_ZHUANHUANCW.Rows.Count > 0)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("文件名");
                dt.Columns.Add("水表编号");
                dt.Columns.Add("错误信息");

                foreach (DataRow drold in YS_CB_ZHUANHUANCW.Rows)
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = drold["WJM"];
                    dr[1] = drold["s_shuibiaobh"];
                    dr[2] = drold["S_CUOWUXX"];
                    dt.Rows.Add(dr);
                }
                PageTools.ExportExcel(dt);
                //ExportExcel(dt);
            }
            else
            {
                PageTools.AlertMsg(this, "无数据可导出");
            }

        }
        /// <summary>
        /// 根据条件显示错误日志
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvCuoWuWJM_SelsctIndexChange(object sender, EventArgs e)
        {
            BindgvCuoWu();
        }

        #endregion

        #region 方法
        
        
        /// <summary>
        /// 绑定转换错误
        /// </summary>
        private void BindgvCuoWu()
        {
            if (gvCuoWuWJM.Value == null)
            {
                return;
            }
            string WJM = gvCuoWuWJM.Value == null ? "-1" : gvCuoWuWJM.Value.ToString();
            DataTable YS_CB_ZHUANHUANCW = _spHelper.P_YS_CB_ZHUANHUANCWCX("", WJM).Tables[0];
            MyDataTable = YS_CB_ZHUANHUANCW;
            gvCuoWu.DataSource = MyDataTable;
            gvCuoWu.DataBind();
            if (YS_CB_ZHUANHUANCW != null && YS_CB_ZHUANHUANCW.Rows.Count > 0)
            {
                lblCuoWuSM.Text = string.Format("共有{0}条记录", YS_CB_ZHUANHUANCW.Rows.Count);
            }
        }

        /// <summary>
        /// 导出excel
        /// </summary>
        /// <param name="dt"></param>
        //private void ExportExcel(DataTable dt)
        //{
        //    //创建Excel对象

        //    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
        //    excel.Application.Workbooks.Add(true);
        //    excel.Visible = true;
        //    //生成字段，即添加表头
        //    for (int i = 0; i < dt.Columns.Count; i++)
        //    {
        //        excel.Cells[1, i + 1] = dt.Columns[i].ColumnName;
        //    }
        //    //填充内容
        //    for (int i = 0; i < dt.Rows.Count; i++)
        //    {
        //        for (int j = 0; j < dt.Columns.Count; j++)
        //        {
        //            if (dt.Rows[i][j] == null)
        //            {
        //                excel.Cells[i + 2, j + 1] = "";
        //            }
        //            else
        //            {
        //                excel.Cells[i + 2, j + 1] = dt.Rows[i][j].ToString();
        //            }
        //        }
        //    }

        //}
        private void BindgvCuoWuWJM()
        {
            string ym = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());
            
            DataTable YS_CB_ZHUANHUANCW = _spHelper.P_YS_CB_ZHUANHUANCWCX("", "").Tables[0];
            gvCuoWuWJM.DataSource = YS_CB_ZHUANHUANCW;
            gvCuoWuWJM.ValueField = "s_cuowubh";
            gvCuoWuWJM.TextField = "wjm";
            gvCuoWuWJM.DataBind();
            gvCuoWuWJM.SelectedIndex = 0;
        }
        #endregion
    }
}